Page({
  data: {
    isSelecting: false,
    weekInfo: [],
    userCourseList: [],
    week: ["第一周", "第二周", "第三周","第四周", "第五周", "第六周", "第七周", "第八周", "第九周", "第十周", "第十一周", "第十二周",
     "第十三周","第十四周","第十五周","第十六周","第十七周","第十八周","第十九周","第二十周"],
    weekday: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
    searchValue: "",
    colorArray: [],
    url:getApp().globalData.url,
    save:0,
   
  },
  //第一次加载
  onLoad() {
   
      if(getApp().globalData.judge==false){
         var judge=getApp().globalData.judge
        this.myLoadFunction();
        getApp().globalData.judge=true
      }  
  },
  myLoadFunction(){
    // this.setData({
    //   url: getApp().globalData.url,
    // });
   this.firstLoadData();
    dd.stopPullDownRefresh() //刷新完成后停止下拉刷新动效
  },

  onReady() {
    // 页面加载完成
    dd.showToast({
      type: "success",
      content: '加载成功',
      duration: 2000
    })
    let $this=this;
    setTimeout(function(){
      $this.Scroll($this.data.save);
    },500)
  },

  get(res){
   
       let $this = this;
       var count=0,record=0;
       var tian=this.data.weekInfo[1];
       for(let j = 1;j <= tian-1;j++){
        record=0;
         for(let i = 0;i < res.data.data.length;i++){
              if(res.data.data[i][i+1][j].length > 0){
                 count++;
                 record++;  
              }
          }
          // if(record==0){
          //   dd.showToast({
          //    type: "exception",
          //    content: '没有课', 
          //   duration: 2000
          // })
          // }
       }
      return count;
  },
  onPullDownRefresh() {
   
  },


  //这时候是跳转来的
  onShow() {
   
    this.getOthersData();
    this.setData({ isSelecting:false})
  
  },

  //初次加载的时候
  firstLoadData() {
    //先处理好当天的日期
    this.setData({
      weekInfo: getApp().globalData.weekInfo
    });
    //第一次加载需要请求当前userId的今日课表
    this.setData({
      id: getApp().globalData.id
    })
   
    this.getCourseList();
  },

  //跳转来对应的查询别人方法
  getOthersData() {
    let search = getApp().globalData.searchValue;
    if (search != "") {
      //先处理好当天的日期
      this.setData({
        weekInfo: getApp().globalData.weekInfo
      });
      this.setData({
        id: search
      })
      getApp().globalData.searchValue = "";
    
      //这里做标记，标识查询别人课表
      this.getCourseList();
   
    }
  },

  //发送请求获取数据
  getCourseList() {
    let $this = this;
    console.log(this.data.weekInfo[0])
    dd.httpRequest({
      url: $this.data.url+"/course/getCourseByWeek",
      method: 'GET',
      data: {
        ddId: this.data.id,
        week: this.data.weekInfo[0]+1
      },
      success: function(res) {
        $this.setData({
          save: $this.get(res),

        });
         console.log(res)
    
        let len = $this.data.userCourseList.length;
        if(res.data.code == 0){
          $this.$spliceData({ userCourseList: [0, len] })
          for(let j = 1;j <= 7;j++){
            for(let i = 0;i < res.data.data.length;i++){
              if(res.data.data[i][i+1][j].length > 0){
                $this.$spliceData({ userCourseList: [$this.data.userCourseList.length, 0,res.data.data[i][i+1][j][0]] })
              }
            }
          }

          $this.getRandomColor()
          
          for(let i = 0;i < $this.data.userCourseList.length;i++){
            let weekday = 'userCourseList['+i+'].weekday'
            if($this.data.userCourseList[i].weekday == 1){
              // 改变数组内某一属性的值
              $this.setData({
                [weekday]:'一'
              })
            }else if($this.data.userCourseList[i].weekday == 2){
              $this.setData({
                [weekday]:'二'
              })
            }else if($this.data.userCourseList[i].weekday == 3){
              $this.setData({
                [weekday]:'三'
              })
            }else if($this.data.userCourseList[i].weekday == 4){
              $this.setData({
                [weekday]:'四'
              })
            }else if($this.data.userCourseList[i].weekday == 5){
              $this.setData({
                [weekday]:'五'
              })
            }else if($this.data.userCourseList[i].weekday == 6){
              $this.setData({
                [weekday]:'六'
              })
            }else if($this.data.userCourseList[i].weekday == 7){
              $this.setData({
                [weekday]:'日'
              })
            }
          }
       
        }else{
          $this.$spliceData({ userCourseList: [0, len] })
          dd.showToast({
            type: "exception",
            content: '没有课',
            duration: 2000
          })
        }
      },
      fail: function(res) {

        dd.showToast({
          type: "exception",
          content: '操作失败，请检查网络',
          duration: 2000
        })
      }
    })
  },
  //根据用户名进行查询用户跳转页面
  handleSearch() {
    if(this.data.searchValue == ""){
      dd.showToast({
        type: "exception",
        content: '用户名不能为空',
        duration: 2000
      })
    }else{
      dd.navigateTo({
        url: "chooseuser/chooseuser?searchValue=" + this.data.searchValue,
      })
    }
  },
  //输入框事件
  bindInputValue(e) {
    this.setData({
      searchValue: e.detail.value,
    })
  },
  //点击日期进入筛选页面
  handleTapSelect() {
    this.setData({ isSelecting: true });
  },
  //筛选框中进行筛选
  handleChangeDay(e) {
    this.setData({
      weekInfo: e.detail.value
    });
  },
  //筛选后点击确定按钮并进行筛选
  handleSelectOk() {
    this.setData({ isSelecting: false });
    //这里需要进行请求获取所选用户的当日所有课表
    this.getCourseList();
  },
  //重置所有筛选条件
  resetSelectRules() {
    this.firstLoadData();
  },
  //获取随机颜色
  getRandomColor() {
    let colorArray = [];
    let len = this.data.userCourseList.length;
    for (let i = 0; i <= 7; i++) {
      colorArray.push('#' + ('00000' + (Math.random() * 0x1000000 << 0).toString(16)).substr(-6));
    }

    for (let i = 0; i < len; i++) {
      this.$spliceData({ colorArray: [0, 0, colorArray[this.data.userCourseList[i].weekday]] })
    }
  },
   Scroll(i){
    dd.pageScrollTo({
      scrollTop:104*(i),
      duration:300
    })
  }


})